# 2. 准备一个空列表，用来放“真正需要打包的 .so”
set(MYEXE_RUNTIME_DEPS)

# 3. 递归解析 app 依赖的所有库
function(collector target)
    # 3-1 跳过非导入型目标（自己写的静态库/对象库等）
    get_target_property(t_type ${target} TYPE)
    if(NOT t_type STREQUAL "INTERFACE_LIBRARY" AND NOT target IN_LIST MYEXE_RUNTIME_DEPS)
        # 3-2 拿到库文件全路径
        get_target_property(loc ${target} IMPORTED_LOCATION_RELEASE)
        if(NOT loc)
            get_target_property(loc ${target} IMPORTED_LOCATION)
        endif()
        # 3-3 只保留 .so 且不是系统目录
        if(loc AND loc MATCHES "\\.so(\\.[0-9]+)*$" AND
           NOT loc MATCHES "^/(usr/)?lib(64)?(/|$)")
            list(APPEND MYEXE_RUNTIME_DEPS "${loc}")
        endif()
    endif()

    # 3-4 继续递归接口依赖
    get_target_property(iface_deps ${target} INTERFACE_LINK_LIBRARIES)
    foreach(dep ${iface_deps})
        if(TARGET ${dep})
            collector(${dep})
        endif()
    endforeach()

    set(MYEXE_RUNTIME_DEPS "${MYEXE_RUNTIME_DEPS}" PARENT_SCOPE)
endfunction()

collector(${MYEXE})

# 4. 去重、打印验证
list(REMOVE_DUPLICATES MYEXE_RUNTIME_DEPS)
message(STATUS ">>> 需要打包的第三方 .so：${MYEXE_RUNTIME_DEPS}")

# 5. 安装/打包时直接引用这个列表
install(FILES ${MYEXE_RUNTIME_DEPS} DESTINATION lib)